*******************************************************************;
***figure2.sas                        				***;
*******************************************************************;
***This program creates data for Figure 2 in the Davis, Grim,   ***;
***Haltiwanger, and Streitwieser 2013 RESTAT paper.             ***;
*******************************************************************;
***Output:                                                      ***;
*** csv/figure2.csv                                             ***;
*******************************************************************;

options ls=80;

****START MACRO CODE****;

%macro stat(var, wt);

data temp1;
	set gs6300;

proc sort data=temp1;
	by year;

proc univariate data=temp1 round=0.001 noprint vardef=wdf;
	var &var;
	by year;
	weight wt&wt;
	output out=&var._&wt p10=p10 mean=mean p90=p90 std=std n=n 
nmiss=nmiss pctlpts=25 75 pctlpre=pct_ pctlname=p25 p75;
	
data &var._&wt (keep = year mean_&wt);
	set &var._&wt;
	mean_&wt = exp(mean)*100;

proc sort data=&var._&wt;
	by year;

%mend stat;

****START PROGRAM****;

data gs6300 (keep = year ppn wt tvs pe lpe_gdp wttvs wtpe);
	set elec.gs6300;

%stat(lpe_gdp, tvs)
%stat(lpe_gdp, pe)

**EIA Data;
*Read in EIA price data;

proc import datafile='csv/pub/import_all.csv'
	out = eiaprice dbms = csv;

proc sort data=eiaprice;
	by year;

data eia_pub (drop = var:);
	set eiaprice;

	if year ne .;

	*Create real prices;
	
	if gdp_defl ne . then do;

		res_price = elec_res/(gdp_defl/100);
		com_price = elec_com/(gdp_defl/100);
		ind_price = elec_ind/(gdp_defl/100);

	end;

	*Create labels;
	
	label year = 'Year';

	label res_price = 'Real Electricity Prices Paid by Residential Consumers (1996 cents/kWh)';
	label com_price = 'Real Electricity Prices Paid by Commercial Consumers (1996 cents/kWh)';
	label ind_price = 'Real Electricity Prices Paid by Industrial Consumers (1996 cents/kWh)';

**Pull together data for Figure 2;

*Limit EIA public data to years and variables in the final plot;

data eia_pub;
	set eia_pub (keep = year res_price com_price ind_price);
	if year ge 1960 and year le 2000;

proc sort data=eia_pub;
	by year;

data figure2;
	merge eia_pub lpe_gdp_tvs lpe_gdp_pe;
	by year;

proc export data=figure2 outfile="csv/figure2.csv"
	dbms=csv replace;
	
run;


	 

